package cn.panxing.server.search;


import java.io.File;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.FSDirectory;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
/**
 * This class is used to demonstrate the 
 * process of searching on an existing 
 * Lucene index
 *
 */
public class TxtFileIndexerTestBak {
	public static void main(String[] args) throws Exception{
	    String queryStr = "台 + 湾 ";
	    //This is the directory that hosts the Lucene index
        File indexDir = new File("/home/allen/index");
        FSDirectory directory = FSDirectory.getDirectory(indexDir,false);
        IndexSearcher searcher = new IndexSearcher(directory);
        if(!indexDir.exists()){
        	System.out.println("The Lucene index is not exist");
        	return;
        }
        QueryParser multiQuery = new QueryParser("contents", new StandardAnalyzer() );
        
        
        /*
         * 
         Term term = new Term("contents",queryStr.toLowerCase());
        TermQuery luceneQuery = new TermQuery(term);
        BooleanQuery multiQuery = new BooleanQuery();
        multiQuery.add(luceneQuery, BooleanClause.Occur.MUST );
        multiQuery.add(new TermQuery(new Term("contents","国")), BooleanClause.Occur.MUST );
        */
        
        Hits hits = searcher.search(multiQuery.parse(queryStr));
        for(int i = 0; i < hits.length(); i++){
        	Document document = hits.doc(i);
        	System.out.println("File: " + document.get("path"));
        }
	}
}
